package com.kehutong.auth.util;

import com.kehutong.common.urlconfig.WebChatUrlConfig;
import org.coraframework.http.HttpClient;
import org.coraframework.inject.Singleton;
import org.coraframework.json.JSON;
import org.coraframework.json.JSONObject;
import org.coraframework.util.CallBack;

/**
 * @author: liukunlong
 * @date: 2021-04-07 14:13
 */
@Singleton
public class WebChatUrlConfigUtil extends WebChatUrlConfig {

    /**
     * get请求,返回json
     * @param page
     * @param callBack
     * @return
     * @throws Exception
     */
    public JSONObject get(String page, CallBack<HttpClient> callBack) throws Exception {
        final HttpClient httpClient = HttpClient.create(getUrl() + page);
        callBack.call(httpClient);

        String text = httpClient.get();
        if (httpClient.getResponseCode() != 200) {
            return httpError(httpClient.getResponseCode(), "http状态错误");
        }

        return JSON.parseObject(text);
    }
}
